﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.Text;

namespace Thksoft.MainAspx
{
    public partial class Favorite : BasePage
    {
        public int Paras = 0;
        private Sys_FavoriteBLL ltb = new Sys_FavoriteBLL();
        private Sys_ModuleBLL smb = new Sys_ModuleBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string url = Request.QueryString["ID"].ToString();

                String[] arry = url.Split('=');
                var urlId = arry[arry.Length - 1];
                Sys_Module module = smb.GetInfoById(urlId);

                if (url == "0")
                {
                    SELECT.Style.Add("display", "block");
                    Bind();
                }
                else
                {
                    txtName.Text = module.MODULE_NAME;
                    txtUrl.Text = module.MODULE_PATH;
                    txtDate.Text = DateTime.Now.ToString();
                    INSERT.Style.Add("display", "block");
                }
            }
        }

        //时间转换
        //private string GetDate(string date)
        //{
        //    string t = "";
        //    try
        //    {
        //        DateTime time = Convert.ToDateTime(date.ToString());

        //        decimal mouth = decimal.Parse(time.Month.ToString());
        //        decimal day = decimal.Parse(time.Day.ToString());
        //        if (mouth < 10 && day >= 10)
        //        {
        //            t = "[ 0" + mouth + "-" + day + " ]";
        //        }
        //        else if (day < 10 && mouth >= 10)
        //        {
        //            t = "[ " + mouth + "-0" + day + " ]";
        //        }
        //        else if (day < 10 && mouth < 10)
        //        {
        //            t = "[ 0" + mouth + "-0" + day + " ]";
        //        }
        //        else
        //        {
        //            t = "[ " + mouth + "-" + day + " ]";
        //        }
        //    }
        //    catch
        //    {
        //        DateTime time = DateTime.Now;
        //        decimal mouth = decimal.Parse(time.Month.ToString());
        //        decimal day = decimal.Parse(time.Day.ToString());
        //        if (mouth < 10 && day >= 10)
        //        {
        //            t = "[ 0" + mouth + "-" + day + " ]";
        //        }
        //        else if (day < 10 && mouth >= 10)
        //        {
        //            t = "[ " + mouth + "-0" + day + " ]";
        //        }
        //        else if (day < 10 && mouth < 10)
        //        {
        //            t = "[ 0" + mouth + "-0" + day + " ]";
        //        }
        //        else
        //        {
        //            t = "[ " + mouth + "-" + day + " ]";
        //        }
        //    }
        //    return t;
        //}

        private void Bind()
        {
            string InerHtmls = " <div ><ul>";
            //查询待办信息
            DataTable temp = ltb.GetDataAll(CurrentUserID);
            if (temp != null)
            {
                Label1.Text = temp.Rows.Count.ToString();
            }

            #region 收藏

            if (temp != null && temp.Rows.Count > 0)
            {
                for (int i = 0; i < temp.Rows.Count; i++)
                {
                    string stateDue = "";

                    string str = "【" + temp.Rows[i]["C_FAVORITE_NAME"].ToString() + "】'" + temp.Rows[i]["C_FAVORITE_URL"].ToString() + "' " + temp.Rows[i]["D_FAVORITE_DATE"].ToString() + "收藏" + stateDue;

                    str = SubStr(str);//GetDate(temp.Rows[i]["D_FAVORITE_DATE"].ToString()).ToString()
                    InerHtmls += "<li><span>" + Convert.ToDateTime(temp.Rows[i]["D_FAVORITE_DATE"]).ToString("[MM-dd]") + "<img id='" + temp.Rows[i]["N_FAVORITE_ID"] + "' src='../Images/delete.gif' runat='server' onclick='deleteid(" + temp.Rows[i]["N_FAVORITE_ID"] + ");' title='取消收藏' width='10px' height='7px'/></span><a href='#' onclick=\"achlick('" + temp.Rows[i]["C_FAVORITE_URL"] + "');\">" + str + "";
                    //href='/" + temp.Rows[i]["C_FAVORITE_URL"].ToString() + "'
                    //href=\"javascript:OpenUrl('" + temp.Rows[i]["C_FAVORITE_URL"].ToString() + "')\"
                    DateTime time = DateTime.Parse(temp.Rows[i]["D_FAVORITE_DATE"].ToString()).AddDays(100);//100天以内的有图标
                    DateTime now = DateTime.Now;
                    if (time >= now)
                    {
                        InerHtmls += "<img src='../Images/main_right/new.gif'/> ";
                    }
                    InerHtmls += "</a></li>";
                }
            }

            #endregion 收藏

            InerHtmls += "</ul></div> ";
            div1.InnerHtml = InerHtmls;
        }

        /// <summary>
        /// 截取前30字符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private string SubStr(string str)
        {
            if (str.Length > 50)
            {
                str = str.Substring(0, 50) + "...";
            }
            return str;
        }

        protected void btnBaocun_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text.ToString()))
            {
                MessageBox.Show(this, "请输入收藏名称");
                return;
            }
            if (string.IsNullOrEmpty(txtUrl.Text.ToString()))
            {
                MessageBox.Show(this, "请输入收藏路径");
                return;
            }
            if (string.IsNullOrEmpty(txtDate.Text.ToString()))
            {
                MessageBox.Show(this, "请输入收藏时间");
            }
            Sys_Favorite entity;

            #region 新增

            entity = new Sys_Favorite
            {
                C_FAVORITE_NAME = txtName.Text.ToString(),
                C_FAVORITE_URL = txtUrl.Text.ToString(),
                D_FAVORITE_DATE = Convert.ToDateTime(txtDate.Text.ToString())
            };
            if (ltb.INSERT(entity, CurrentUserName, CurrentUserID))
            {
                MessageBox.ShowAndRedirect(this, "新增成功", "/MainAspx/Favorite.aspx?IF_EDIT=TRUE&ID=" + entity.N_FAVORITE_ID.ToString());
            }
            else
            {
                MessageBox.Show(this, "新增失败");
            }

            #endregion 新增
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            var id = HiddenField1.Value;
            if (ltb.DELETE(id, CurrentUserCompanyName, CurrentUserID))
            {
                MessageBox.ShowAndRedirect(this, "删除成功", Request.RawUrl);
            }
            else
            {
                MessageBox.Show(this, "删除失败");
            }
        }

        protected void btnTiaoZhuang_Click(object sender, EventArgs e)
        {
            var script = new StringBuilder();
            var url = HiddenField2.Value;
            DataTable dt = ltb.GetUrlgetTable1(url, CurrentUserCompanyName, CurrentUserID);
            if (dt != null)
            {
                script.AppendLine("top.Class_ItemCard_OprnNew.Run_ItemCard('" + dt.Rows[0]["module_id"].ToString() + "','" + dt.Rows[0]["c_favorite_name"].ToString() + "','" + "/" + url + "?model_id=" + dt.Rows[0]["module_id"].ToString() + "','0');window.close();");
                //script.AppendLine("window.close();");
                MessageBox.ResponseScript(this, script.ToString());
            }
        }
    }
}